package com.farsunset.cim.nio.model;

import com.alibaba.fastjson.JSON;

public class ObjectConvertUtil {

	// token 验证类的转换
	public static String convertModel(SecureModel secureModel) {
		RecvieMessage recevie = new RecvieMessage();
		recevie.setData(JSON.toJSONString(secureModel));
		recevie.setMsgType(Event.MESSAGE_TYPE_SECURE_MODEL);
		return JSON.toJSONString(recevie);
	}

	// 返回数据类的转换
	public static String convertModle(ResponseFile response) {
		RecvieMessage recevie = new RecvieMessage();
		recevie.setData(JSON.toJSONString(response));
		recevie.setMsgType(Event.MESSAGE_TYPE_RESPONSE_FILE);

//		log.info("ser-ResponseFile:" + JSON.toJSONString(recevie));
		return JSON.toJSONString(recevie);
	}
	
	// 解码器模型转换
	public static Object convertModle(String recviejson) {
		RecvieMessage recvie = (RecvieMessage) JSON.parseObject(recviejson,
				RecvieMessage.class);
		Object obj = null;
		switch (recvie.getMsgType()) {
		case Event.MESSAGE_TYPE_SECURE_MODEL:
			obj = (SecureModel) JSON.parseObject(recvie.getData().toString(),
					SecureModel.class);
			break;
		case Event.MESSAGE_TYPE_REQUEST_FILE:
			break;
		case Event.MESSAGE_TYPE_RESPONSE_FILE:
			obj = (ResponseFile) JSON.parseObject(recvie.getData().toString(),
					ResponseFile.class);
			break;
		}
		return obj;
	}

	public static String request(Object obj) {
		if (obj instanceof SecureModel) {
			SecureModel secureModel = (SecureModel) obj;
			return convertModel(secureModel);
		} else if (obj instanceof ResponseFile) {
			ResponseFile responseFile = (ResponseFile) obj;
			return convertModle(responseFile);
		} else {
			return null;
		}
	}

}
